package org.alwayssuper.protocol.basics;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.alwayssuper.netmc.core.model.Message;
import org.alwayssuper.netmc.session.Session;
import org.alwayssuper.protostar.annotation.Field;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class GBTMessage implements Message {

    //带人16 25 版本
    @Field(length = 1, desc = "命令标识",version = { 0, 1 })
    protected int messageId;

    @Field(length = 1, desc = "应答标识")
    protected int responseType;

    @Field(length = 17, charset = "ASCII", desc = "唯一识别码")
    protected String clientId;

    @Field(length = 1, desc = "数据单元加密方式")
    protected int encryptionType;

    @Field(length = 2, desc = "数据单元长度")
    protected int payloadLength;

    /** bcc校验 */
    protected boolean verified = true;


    /** 协议版本 */
    protected int protocolVersion;
    @JsonIgnore
    protected transient Session session;
    @JsonIgnore
    protected transient ByteBuf payload;

    public GBTMessage copyBy(GBTMessage that){
        this.setMessageId(that.getMessageId());
        this.setClientId(that.getClientId());
        this.setEncryptionType(that.getEncryptionType());
        this.setProtocolVersion(that.getProtocolVersion());
        return this;
    }

    @Override
    public int getSerialNo() {
        return 0;
    }
    /** 下行消息的流水号设置 */
    public void setSerialNo(Integer serialNo) {}

    public int reflectMessageId() {
        if (messageId != 0)
            return messageId;
        return reflectMessageId(this.getClass());
    }

    public static int reflectMessageId(Class<?> clazz) {
        org.alwayssuper.protostar.annotation.Message messageType = clazz.getAnnotation(org.alwayssuper.protostar.annotation.Message.class);
        if (messageType != null && messageType.value().length > 0)
            return messageType.value()[0];
        return 0;
    }

}
